#! /bin/bash
#
# chkconfig: 23456 64 36
# description: despagent
# processname: despagent

CURPATH="/iflytek/ap/desp/agent"
#CURPATH=$(cd `dirname $0`; pwd)
AGENTSERVICE="desp-agent"
INITPATH="/etc/init.d"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${CURPATH}/conf/thirdLibs
# export LANG=en_US.UTF-8
export MALLOC_ARENA_MAX=1

. /etc/profile
if [ -e "~/.bash_profile" ];then
	. ~/.bash_profile
elif [ -e "~/.profile" ];then
	. ~/.profile
fi


#判断系统发行版本大版本号，centos 6、centos 7
release_el6=`uname -a|awk '{print $3}'|grep -v grep|grep el6|wc -l`
release_el7=`uname -a|awk '{print $3}'|grep -v grep|grep el7|wc -l`

#判断系统发行版本大版本号，centos 6、centos 7

Linux_release=`cat /etc/redhat-release|grep -v grep|grep release|awk -F 'release' '{print $2}'|sed s/[[:space:]]//g|awk -F '.' '{print $1}'`

if [ -z $Linux_release ] ; then
	if [ $release_el6 -eq 1 ] ; then
	echo "当前系统发行版本为：el6"
	sleep 2
	Linux_release=6
	fi
	if [ $release_el7 -eq 1 ] ; then
	echo "当前系统发行版本为：el7"
	sleep 2
	Linux_release=7
	fi

fi

#获取当前系统版本号
lsb_release -d > /dev/null 2>&1
if [ $? -eq 0 ] ;
then 
Linux_version=`lsb_release -d |grep Description |awk -F ":" '{print $2}'`
else
Linux_version=`cat /etc/redhat-release|grep -v grep|grep release`
fi


isSystemctl="false"
if [[ -e "/lib/systemd/system/" && $Linux_release =~ "7" ]];then
    isSystemctl="true"
fi

chmod 700 -R ${CURPATH}
#FLAGEID=$(ps aux | grep desp-agent.jar |grep -v grep|grep -v desp-agent.sh|grep jar| awk '{print $2}')
FLAGEID=`echo $(ps aux|grep -v grep|grep -v desp-agent.sh|grep jar|grep -E "desp-agent.jar|desp-agent-ice.jar"| awk '{print $2}')`

cd ${CURPATH}
start(){
    if [ "$FLAGEID" == "" ];then
        echo "Starting desp-agent..."
#		nohup java -jar desp-agent.jar >/dev/null 2>&1 &
        exec java -jar desp-agent.jar 2>&1 >/dev/null &
        echo "desp-agent started!"
    else
        echo "desp-agent already started..."
    fi
	return 0
}

stop(){
    if [ "$FLAGEID" == "" ];then
        echo "desp-agent already stoped..."
    else
        echo "Stopping desp-agent..."
		kill -9 ${FLAGEID}
        echo "desp-agent stoped!"
    fi
	return 0
}

restart(){
    echo "restarting desp-agent..."
    while [[ "$FLAGEID" != "" ]]
    do
		kill -9 ${FLAGEID}
		FLAGEID=$(ps aux | grep "desp-agent.jar" |grep -v grep | awk '{print $2}' | head -n 1)
    done
#    nohup java -jar desp-agent.jar >/dev/null 2>&1 &
    exec java -jar desp-agent.jar 2>&1 >/dev/null &
    echo "desp-agent restarted!"
	return 0
}

registerService(){
	CHKCONFIG=`chkconfig --list 2>/dev/null`
	TEMP=$(echo "${CHKCONFIG}" | grep $AGENTSERVICE)
	if [ -z "$TEMP" ];then
		ln -s ${CURPATH}/desp-agent.sh ${INITPATH}/${AGENTSERVICE}
		chmod +x ${INITPATH}/${AGENTSERVICE}
		chkconfig --add ${AGENTSERVICE}
		chkconfig --level 23456 ${AGENTSERVICE} on
		echo "install service desp-agent success!"
	fi
	return 0
}

selfStarting(){
    if [ -e "/etc/rc.local" ];then
	    if [ -x "/etc/rc.local" ];then
		    echo "continue....." >> ./redeploy.log
	    else
		    chmod 755 /etc/rc.local
	    fi
	fi
	
	if [ -e "/etc/rc.d/rc.local" ];then
	    if [ -x "/etc/rc.d/rc.local" ];then
		    echo "continue....." >> ./redeploy.log
	    else
		    chmod 755 /etc/rc.d/rc.local
	    fi
	fi

	if cat /etc/rc.local | grep './desp-agent.sh start' > /dev/null;then
		echo "shell exists!" >> ./redeploy.log
	elif cat /etc/rc.local | grep 'exit 0' | grep -v "#" > /dev/null;then
		sed -i 's/exit 0/cd \/iflytek\/ap\/\/desp\/agent\//g' /etc/rc.local
		echo "./desp-agent.sh start" >> /etc/rc.local
		echo "exit 0" >> /etc/rc.local
		echo "add exit 0 to rc.load success!" >> ./redeploy.log
	else
		echo "cd $CURPATH" >> /etc/rc.local
		echo "./desp-agent.sh start" >> /etc/rc.local
		echo "add to rc.local success!" >> ./redeploy.log
	fi
	return 0
}

if [ $isSystemctl == "true" ];then
	cp -f /iflytek/ap/desp/agent/desp-agent.service /lib/systemd/system/desp-agent.service
	chmod 777 /lib/systemd/system/desp-agent.service
	`systemctl enable desp-agent.service`
else
    registerService
    selfStarting
fi

# See how we were called.
case "$1" in
    start)
	    if [[ $isSystemctl == "true" && -e "/lib/systemd/system/desp-agent.service" ]];then
		    `systemctl start desp-agent.service`
		else
		    start
		fi
		;;
    stop)
		if [[ $isSystemctl == "true" && -e "/lib/systemd/system/desp-agent.service" ]];then
		    `systemctl stop desp-agent.service`
		else
		    stop
		fi
		;;
    restart)
		if [[ $isSystemctl == "true" && -e "/lib/systemd/system/desp-agent.service" ]];then
		    `systemctl restart desp-agent.service`
		else
		    restart
		fi
        ;;
	service_start)
		start
		;;
    service_stop)
		stop
		;;
    service_restart)
		restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart}"
		exit 1
esac


exit $?


